package com.hnucm.c202001020216.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;

import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.hnucm.c202001020216.DataApplication;
import com.hnucm.c202001020216.R;
import com.hnucm.c202001020216.api.LoginApi;
import com.hnucm.c202001020216.databinding.ActivityLoginBinding;
import com.hnucm.c202001020216.pojo.Login;
import com.hnucm.c202001020216.pojo.User;
import com.hnucm.c202001020216.utils.RetrofitUtils;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author starr
 */
public class LoginActivity extends AppCompatActivity {

    private final static String TAG = "LoginActivity";
    ObservableField<String> headUrl = new ObservableField<>();
    ActivityLoginBinding loginBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginBinding = DataBindingUtil.setContentView(this, R.layout.activity_login);

        //设置沉浸式布局
        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .init();

        //设置头像图片的url
        headUrl.set("https://starrylixu.oss-cn-beijing.aliyuncs.com/picgo/202401291613856.jpg");

        loginBinding.setHeadurl(headUrl);

        //用户注册
        loginBinding.textView5.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
            startActivity(intent);
        });

        //游客登录
        loginBinding.textView4.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            Login.UserDTO user = new Login.UserDTO();
            user.userId = 3;
            user.username = "游客";
            user.mobile = "游客";
            user.userPic = "https://starrylixu.oss-cn-beijing.aliyuncs.com/picgo/202401291613856.jpg";
            DataApplication.dataUserDTO = user;
            finish();
            startActivity(intent);
        });

        //验证登录
        loginBinding.loginBtnCon.setOnClickListener(
                (view) -> {
                    User user = new User();
                    user.mobile = loginBinding.editTextTextPersonName.getText().toString();
                    user.password = loginBinding.editTextTextPassword.getText().toString();
                    String dataBody = new Gson().toJson(user);
                    LoginApi api = RetrofitUtils.getRetrofit(DataApplication.HttpURL).create(LoginApi.class);
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), dataBody);
                    Call<Login> call = api.validateUserLogin(body);
                    call.enqueue(new Callback<Login>() {
                        @Override
                        public void onResponse(@NonNull Call<Login> call, @NonNull Response<Login> response) {
                            Log.e(TAG, "onResponse: " + response.body());
                            //成功登录
                            assert response.body() != null;
                            if ("success".equals(response.body().msg)) {
                                DataApplication.dataUserDTO = (response.body().user);
                                finish();
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                Log.i("login", "onCreate: login");
                                startActivity(intent);
                            } else {
                                Toast.makeText(LoginActivity.this, "账号或密码错误！", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onFailure(@NonNull Call<Login> call, @NonNull Throwable t) {
                            Toast.makeText(LoginActivity.this, "网络错误！", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
        );
    }

    public void onClickPhone(View view) {
        Intent intent = new Intent(this, LoginPhoneActivity.class);
        startActivity(intent);
    }
}